// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <pollux/core/plan_node.h>
#include <pollux/vector/decoded_vector.h>

namespace kumo::pollux::connector::hive {

class HivePartitionFunction : public core::PartitionFunction {
 public:
  HivePartitionFunction(
      int numBuckets,
      std::vector<int> bucketToPartition,
      std::vector<column_index_t> keyChannels,
      const std::vector<VectorPtr>& constValues = {});

  HivePartitionFunction(
      int numBuckets,
      std::vector<column_index_t> keyChannels,
      const std::vector<VectorPtr>& constValues = {})
      : HivePartitionFunction(
            numBuckets,
            {},
            std::move(keyChannels),
            constValues) {}

  ~HivePartitionFunction() override = default;

  std::optional<uint32_t> partition(
      const RowVector& input,
      std::vector<uint32_t>& partitions) override;

  const std::vector<int>& testingBucketToPartition() const {
    return bucketToPartition_;
  }

 private:
  // Precompute single value hive hash for a constant partition key.
  void precompute(const BaseVector& value, size_t column_index_t);

  void hash(
      const DecodedVector& values,
      TypeKind typeKind,
      const SelectivityVector& rows,
      bool mix,
      std::vector<uint32_t>& hashes,
      size_t poolIndex);

  template <TypeKind kind>
  void hashTyped(
      const DecodedVector& /* values */,
      const SelectivityVector& /* rows */,
      bool /* mix */,
      std::vector<uint32_t>& /* hashes */,
      size_t /* poolIndex */) {
    POLLUX_UNSUPPORTED(
        "Hive partitioning function doesn't support {} type",
        TypeTraits<kind>::name);
  }

  // Helper functions to retrieve reusable memory from pools.
  DecodedVector& getDecodedVector(size_t poolIndex = 0);
  SelectivityVector& getRows(size_t poolIndex = 0);
  std::vector<uint32_t>& getHashes(size_t poolIndex = 0);

  const int numBuckets_;
  const std::vector<int> bucketToPartition_;
  const std::vector<column_index_t> keyChannels_;

  // Pools of reusable memory.
  std::vector<std::unique_ptr<std::vector<uint32_t>>> hashesPool_;
  std::vector<std::unique_ptr<SelectivityVector>> rowsPool_;
  std::vector<std::unique_ptr<DecodedVector>> decodedVectorsPool_;
  // Precomputed hashes for constant partition keys (one per key).
  std::vector<uint32_t> precomputedHashes_;
};
} // namespace kumo::pollux::connector::hive
